//
//  PopoverAnimate.swift
//  WeiBo
//
//  Created by LTX on 2016/11/4.
//  Copyright © 2016年 LTX. All rights reserved.
//

import UIKit

class PopoverAnimate: NSObject {
    
    //是否以弹出
    var isPresented = false
    
    var presentFrame = CGRect.zero
    
    var callBack  : ((_ isShow : Bool)->())?
    
    //如果自定义了init方法 而没有对默认的init方法进行重写，自定义的init方法会覆盖默认的init方法
    override init() {
        
    }
    
    //自定义init方法
    init(ShowComplete: @escaping (_ isShow : Bool)->()) {
        
        self.callBack = ShowComplete
    }
    
}


// MARK:- popoverView代理方法
extension PopoverAnimate: UIViewControllerTransitioningDelegate{
    
    // 设置弹出View尺寸
    func presentationController(forPresented presented: UIViewController, presenting: UIViewController?, source: UIViewController) -> UIPresentationController? {
        
        //返回自定义的WBPresentationController
        let presentation = WBPresentationController(presentedViewController: presented, presenting: presenting)
        presentation.Frame = presentFrame
        return presentation
        
        
    }
    
    
    // 自定义弹出的动画
    func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        
        isPresented = true
        
        callBack!(isPresented)
        
        return self
    }
    
    // 自定义消失动画
    func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        
        isPresented = false
        callBack!(isPresented)

        return self
    }
    
    
    
}


// MARK:- 弹出和消失动画的代理
extension PopoverAnimate: UIViewControllerAnimatedTransitioning{
    
    // 动画执行时间
    func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
        return 0.25
    }
    
    // 获取转场动画的上下文:可以通过该上下文获取到弹出和消失的view
    // UITransitionContextViewKey.to 获取弹出的View
    // UITransitionContextViewKey.from 获取消失的view
    func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
        
        isPresented ? animateForPresented(using: transitionContext) : animateForDismiss(using: transitionContext)
        
    }
    
    
    
    // MARK:- 弹出动画
    func animateForPresented(using transitionContext: UIViewControllerContextTransitioning) {
        
        // 获取弹出的view
        let presentedView = transitionContext.view(forKey: UITransitionContextViewKey.to)
        
        //将弹出的view添加到containerView中
        transitionContext.containerView.addSubview(presentedView!)
        
        //执行动画
        presentedView?.transform = CGAffineTransform(scaleX: 1.0, y: 0.0)
        presentedView?.layer.anchorPoint = CGPoint(x: 0.5, y: 0)
        UIView.animate(withDuration: transitionDuration(using: transitionContext), animations: {
            
            presentedView?.transform = CGAffineTransform.identity
            
        }) { (isFinished) in
            //        //必须告诉上下文动画已完成
            transitionContext.completeTransition(true)
        }
        
    }
    
    
    
    
    // MARK:- 消失动画
    func animateForDismiss(using transitionContext: UIViewControllerContextTransitioning) {
        
        //获取消失的view
        let dismissView = transitionContext.view(forKey: UITransitionContextViewKey.from)
        UIView.animate(withDuration: transitionDuration(using: transitionContext), animations: {
            
            dismissView?.transform = CGAffineTransform(scaleX: 1.0, y: 0.00001)
            
        }) { (_) in
            
            dismissView?.removeFromSuperview()
            transitionContext.completeTransition(true)
        }
    }
    
    
}
